//
//  KDDSectionData.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//
#import "KDDSectionData.h"
#import "KDDModel.h"
@implementation KDDSectionData
{
    NSMutableArray * _cellModelList;
}
@dynamic cellModelList;
@synthesize modelList = _modelList;
KNSCopyingImpl

- (instancetype)init
{
    if (self = [super init])
    {
        _headerFooterCross = YES;
    }
    return self;
}

- (instancetype)initWithSectionIndex:(NSInteger)sectionIndex
{
    if (self = [super init])
    {
        _headerFooterCross = YES;
        self.index = sectionIndex;
    }
    return self;
}

- (void)setHeaderModel:(id)headerModel
{
    _headerModel = headerModel;
    _modelList = nil;
}

- (NSArray *)cellModelList
{
    if (_cellModelList == nil)
    {
        _cellModelList = [NSMutableArray array];
    }
    return _cellModelList;
}

- (void)setCellModelList:(NSArray *)cellModelList
{
    _cellModelList = [NSMutableArray arrayWithArray:cellModelList];
    _modelList = nil;
}

- (void)setFooterModel:(id)footerModel
{
    _footerModel = footerModel;
    _modelList = nil;
}

- (NSMutableArray *)cellModelListForUpdate
{
    _modelList = nil;
    return (NSMutableArray *)self.cellModelList;
}

- (NSArray *)modelList
{
    if (_modelList == nil)
    {
        NSMutableArray * modelList = [NSMutableArray array];
        if (_headerModel != nil)
        {
            [modelList addObject:_headerModel];
        }
        if (_cellModelList != nil)
        {
            [modelList addObjectsFromArray:_cellModelList];
        }
        if (_footerModel != nil)
        {
            [modelList addObject:_footerModel];
        }
        _modelList = modelList;
    }
    return _modelList;
}

- (void)setModelList:(NSArray *)modelList
{
    _modelList = [NSMutableArray arrayWithArray:modelList];
}

- (BOOL)isEmpty
{
    return _headerModel == nil && _footerModel == nil && _cellModelList.count == 0;
}

- (BOOL)addModel:(NSObject *)model viewType:(KDDViewType)viewType
{
    [self modelList];
    if (viewType == KDDViewTypeCell)
    {
        if (_footerModel != nil)
        {
            return NO;
        }
        [(NSMutableArray *)self.cellModelList addObject:model];
    }
    else if (viewType == KDDViewTypeHeader)
    {
        if (_modelList.count > 0)
        {
            return NO;
        }
        _headerModel = model;
    }
    else if (viewType == KDDViewTypeFooter)
    {
        if (_footerModel != nil)
        {
            return NO;
        }
        _footerModel = model;
    }
    [(NSMutableArray *)_modelList addObject:model];
    return YES;
}

- (void)clear
{
    _headerModel = nil;
    _cellModelList = [NSMutableArray array];
    _footerModel = nil;
    _modelList = [NSMutableArray array];
}

+ (NSMutableArray<KDDSectionData *> *)copySectionDataList:(NSArray<KDDSectionData *> *)sectionDataList
{
    NSMutableArray<KDDSectionData *> * resultList = [NSMutableArray arrayWithCapacity:sectionDataList.count];
    for (KDDSectionData * sectionData in sectionDataList)
    {
//        [resultList addObject:[sectionData copy]];
        [resultList addObject:[KDDSectionData copySectionData:sectionData]];
    }
    return resultList;
//    return [NSMutableArray arrayWithArray:sectionDataList];
}

+ (KDDSectionData *)copySectionData:(KDDSectionData *)sectionData
{
    KDDSectionData * model = [KDDSectionData new];
    model.index = sectionData.index;
    model.indexTitle = sectionData.indexTitle;
    model.headerModel = sectionData.headerModel;
    model.cellModelList = sectionData.cellModelList;
    model.footerModel = sectionData.footerModel;
    model.headerFooterCross = sectionData.headerFooterCross;
    model.decorationCross = sectionData.decorationCross;
    model.sectionInsets = sectionData.sectionInsets;
    model.minimumLineSpacing = sectionData.minimumLineSpacing;
    model.minimumInteritemSpacing = sectionData.minimumInteritemSpacing;
    model.userInfo = sectionData.userInfo;
    return model;
}

@end

//- (void)setModelList:(NSArray *)modelList
//{
//    _headerModel = nil;
//    _cellModelList = [NSMutableArray arrayWithCapacity:modelList.count];
//    _footerModel = nil;
//    _modelList = [NSMutableArray arrayWithArray:modelList];
//    for (NSObject * model in modelList)
//    {
//        if (model.ddViewType == KDDViewTypeCell)
//        {
//            if (_footerModel != nil)
//            {
//                break;
//            }
//            [_cellModelList addObject:model];
//        }
//        else if (model.ddViewType == KDDViewTypeHeader)
//        {
//            if (_modelList.count > 0)
//            {
//                break;
//            }
//            _headerModel = model;
//        }
//        else if (model.ddViewType == KDDViewTypeHeader)
//        {
//            _footerModel = model;
//            [(NSMutableArray *)_modelList addObject:model];
//            break;
//        }
//        [(NSMutableArray *)_modelList addObject:model];
//    }
//}
